
package com.tideit.husai.ui.watch

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.tideit.husai.R
import com.tideit.husai.databinding.FragmentWatchModeBinding
import com.tideit.husai.databinding.ItemCommentBinding
import java.text.SimpleDateFormat
import java.util.*

class WatchModeFragment : Fragment() {
    private var _binding: FragmentWatchModeBinding? = null
    private val binding get() = _binding!!
    private val comments = mutableListOf<Comment>()
    private lateinit var adapter: CommentAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentWatchModeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 初始化评论列表
        adapter = CommentAdapter(comments)
        binding.rvComments.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = this@WatchModeFragment.adapter
        }

        // 发送评论按钮点击事件
        binding.btnSendComment.setOnClickListener {
            val commentText = binding.etComment.text.toString()
            if (commentText.isNotEmpty()) {
                addComment(commentText)
                binding.etComment.text.clear()
            }
        }
    }

    private fun addComment(text: String) {
        val comment = Comment(
            user = "军事观察员",
            text = text,
            time = SimpleDateFormat("HH:mm", Locale.getDefault()).format(Date())
        )
        comments.add(comment)
        adapter.notifyItemInserted(comments.size - 1)
        binding.rvComments.scrollToPosition(comments.size - 1)
    }

    override fun onDestroyView() {
        _binding = null
        super.onDestroyView()
    }

    private inner class CommentAdapter(private val comments: List<Comment>) :
        RecyclerView.Adapter<CommentAdapter.CommentViewHolder>() {

        inner class CommentViewHolder(val binding: ItemCommentBinding) : 
            RecyclerView.ViewHolder(binding.root)

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentViewHolder {
            val binding = ItemCommentBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
            return CommentViewHolder(binding)
        }

        override fun onBindViewHolder(holder: CommentViewHolder, position: Int) {
            val comment = comments[position]
            holder.binding.tvCommentUser.text = comment.user
            holder.binding.tvCommentText.text = comment.text
            holder.binding.tvCommentTime.text = comment.time
        }

        override fun getItemCount() = comments.size
    }

    data class Comment(
        val user: String,
        val text: String,
        val time: String
    )
}
